package com.infore.operation.controller;

import com.infore.operation.entity.*;
import com.infore.operation.entity.vo.LoginUser;
import com.infore.operation.service.UserService;
import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.FormatUtil;
import com.infore.operation.utils.StatusCode;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:wk
 * @date:Created in 14:22 2018\4\16 0016
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    private CacheManager cacheManager = CacheManager.newInstance();

    /**
     * 登录验证
     * @param userAccount
     * @param userPassword
     * @return
     */
    @PostMapping("/servlet/login.do")
    public AbstractJsonObject login(@RequestParam(value = "userAccount") String userAccount,
                                    @RequestParam(value = "userPassword") String userPassword){
        Ehcache cache = cacheManager.getEhcache("ehCache");
        LoginUser loginUser = userService.login(userAccount,userPassword);
        if (loginUser!=null){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user", loginUser);
            return FormatUtil.queryListObject(map,"登录成功");
        }else {
            return FormatUtil.queryAbstractJsonObject("密码错误！", StatusCode.CODE_ERROR);
        }
    }

    /**
     * 查询下级人员
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/queryLowPson.do")
    public AbstractJsonObject queryLowPson(@RequestParam(value = "userId") String userId) {
        List<Person> list = userService.queryLowPson(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取下级人员数据成功");
    }

    /**
     * 查询上级人员
     * @param userId
     * @param stationId
     * @return
     */
    @RequestMapping("/servlet/queryHeadPson.do")
    public AbstractJsonObject queryHeadPson(@RequestParam(value = "userId") String userId,
                                           @RequestParam(value = "stationId",required = false)Integer stationId) {
        List<Person> list = userService.queryHeadPson(userId,stationId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取下级人员数据成功");
    }

    /**
     * 根据人员获取分公司
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/queryBranchByUserId.do")
    public AbstractJsonObject queryBranchByUserId(@RequestParam(value="userId",required = false) String userId){
        List<Branch> list = userService.queryBranchByUserId(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取分公司数据成功");
    }

    /**
     * 根据公司获取人员
     * @param userId
     * @param branchId
     * @return
     */
    @RequestMapping("/servlet/queryPsonByBranchId.do")
    public AbstractJsonObject queryPsonByBranchId(@RequestParam(value = "userId",required=false) String userId,
                                      @RequestParam(value="branchId",required=false) String branchId){
        List<OpCenPerson> list = userService.queryPsonByBranchId(userId,branchId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取分公司人员数据成功");
    }

    /**
     * 用户修改密码
     * @param userId
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping("/servlet/changePass.do")
    public AbstractJsonObject updatePassword(@RequestParam(value = "userId") String userId,
                                 @RequestParam(value="oldPassword") String oldPassword,
                                 @RequestParam(value="newPassword") String newPassword){
        userService.changePassword(userId,oldPassword,newPassword);
        return  FormatUtil.queryAbstractJsonObject("修改密码成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 上传头像
     * @param userId
     * @param file
     * @return
     */
    @RequestMapping("/servlet/headImage.do")
    public AbstractJsonObject queryHeadImage(@RequestParam(value = "userId") String userId,
                                 @RequestParam(value = "picture")MultipartFile file){
        userService.saveHeadImage(userId,file);
        return FormatUtil.queryAbstractJsonObject("上传图片成功！",StatusCode.CODE_SUCCESS);
    }

    /**
     * 定位
     * @param userId
     * @param lnt
     * @param lat
     * @param address
     * @return
     */
    @RequestMapping("/servlet/GPS.do")
    public AbstractJsonObject saveGPS(@RequestParam(value = "userId") String userId,
                          @RequestParam(value = "lnt") String lnt,
                          @RequestParam(value = "lat") String lat,
                          @RequestParam(value = "address") String address){
        userService.saveGPS(userId,lnt,lat,address);
        return FormatUtil.queryAbstractJsonObject("定位成功！",StatusCode.CODE_SUCCESS);
    }

    /**
     * 获取人员
     * @param userId
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("/servlet/queryPsonByUserId.do")
    public AbstractJsonObject queryPsonByUserId(@RequestParam(value = "userId") String userId,
                                    @RequestParam(value = "pageNum") Integer pageNum,
                                    @RequestParam(value = "keyword") String keyword){
        List<PositionPerson> list = userService.queryPsonByUserId(userId,pageNum,keyword);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取人员成功");
    }

    /**
     * 获取附近的人员
     * @param userId
     * @param lat
     * @param lnt
     * @return
     */
    @RequestMapping("/servlet/NearPson.do")
    public AbstractJsonObject queryNearPson(@RequestParam(value = "userId") String userId,
                                @RequestParam(value = "lat") String lat,
                                @RequestParam(value = "lnt") String lnt){
        List<PositionPerson> list = userService.queryNearPson(userId,lat,lnt);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取附近的人员成功");
    }

    /**
     * 获取验证码
     * @param phoneNum
     * @return
     */
    @RequestMapping(value ={"/verifyCode","/servlet/verifyCode"})
    public AbstractJsonObject verifyCode(@RequestParam(value="phoneNum") String phoneNum) {
        userService.verifyCode(phoneNum);
        return FormatUtil.queryAbstractJsonObject("发送验证码成功！", StatusCode.CODE_SUCCESS);
    }

    /**
     * 根据验证码修改密码
     * @param phoneNum
     * @param code
     * @param password
     * @return
     */
    @RequestMapping(value = {"/checkCodeAndChangePassword","/servlet/checkCodeAndChangePassword"})
    public AbstractJsonObject checkCodeAndChangePassword(@RequestParam(value="phoneNum") String phoneNum,
                                             @RequestParam(value="code") String code,
                                             @RequestParam(value="password") String password) {
        userService.checkCodeAndChangePassword(phoneNum,code,password);
        return FormatUtil.queryAbstractJsonObject("修改密码成功！",StatusCode.CODE_SUCCESS);
    }

    /**
     * 根据用户获取分公司
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/BranchByMtStock.do")
    public AbstractJsonObject queryBranchByMtStock(@RequestParam(value = "userId") String userId){
        List<FaultLevel> list = userService.queryBranchByMtStock(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取分公司成功");
    }
    
}
